/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui;

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

public class EscapeDialog extends JDialog {
	private static final long serialVersionUID = 1581092475048464358L;
	
	private boolean escapable = true;
	private Action escapeAction;

	public EscapeDialog() {
		super();
	}
	public EscapeDialog(Frame owner) {
		super(owner);
	}
	public EscapeDialog(Dialog owner) {
		super(owner);
	}
	public EscapeDialog(Window owner) {
		super(owner);
	}
	public EscapeDialog(Frame owner, boolean modal) {
		super(owner, modal);
	}
	public EscapeDialog(Frame owner, String title) {
		super(owner, title);
	}
	public EscapeDialog(Dialog owner, boolean modal) {
		super(owner, modal);
	}
	public EscapeDialog(Dialog owner, String title) {
		super(owner, title);
	}
	public EscapeDialog(Window owner, ModalityType modalityType) {
		super(owner, modalityType);
	}
	public EscapeDialog(Window owner, String title) {
		super(owner, title);
	}
	public EscapeDialog(Frame owner, String title, boolean modal) {
		super(owner, title, modal);
	}
	public EscapeDialog(Dialog owner, String title, boolean modal) {
		super(owner, title, modal);
	}
	public EscapeDialog(Window owner, String title, ModalityType modalityType) {
		super(owner, title, modalityType);
	}
	public EscapeDialog(Frame owner, String title, boolean modal,
			GraphicsConfiguration gc) {
		super(owner, title, modal, gc);
	}
	public EscapeDialog(Dialog owner, String title, boolean modal,
			GraphicsConfiguration gc) {
		super(owner, title, modal, gc);
	}
	public EscapeDialog(Window owner, String title, ModalityType modalityType,
			GraphicsConfiguration gc) {
		super(owner, title, modalityType, gc);
	}
	
	@Override
	protected JRootPane createRootPane() {
		JRootPane root = super.createRootPane();
		installEscapAction(root);
		return root;
	}
	
	@Override
	protected void setRootPane(JRootPane root) {
		installEscapAction(root);
		super.setRootPane(root);
	}
	
	private Action createEscapeAction() {
		if(escapeAction == null) {
			escapeAction = new AbstractAction() {
				private static final long serialVersionUID = 
					-2825731321588553895L;
				@Override
				public void actionPerformed(ActionEvent e) {
					if(isEscapable()) {
						dispose();
					}
				}
			};
		}
		
		return escapeAction;
	}
	
	private void installEscapAction(JRootPane root) {
		KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
		
		InputMap inputMap = root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		inputMap.put(stroke, "dispose");
		root.getActionMap().put("dispose", createEscapeAction());
	}
	
	public boolean isEscapable() {
		return escapable;
	}
	
	public void setEscapable(boolean b) {
		escapable = b;
	}
}
